package com.andy.qhb.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.andy.qhb.base.PageUtil;
import com.andy.qhb.base.PageVO;
import com.andy.qhb.base.RespVO;
import com.andy.qhb.base.form.FormField;
import com.andy.qhb.base.table.PageStructure;
import com.andy.qhb.base.table.Search;
import com.andy.qhb.base.table.TableHeader;
import com.andy.qhb.base.table.Tool;
import com.andy.qhb.dao.DaoUtils;
import com.andy.qhb.dao.vo.Where;
import com.andy.qhb.entity.UsrGameInfo;
import com.andy.qhb.entity.UsrTixianLog;
import com.andy.qhb.entity.UsrUser;
import com.google.common.collect.Maps;

@Controller
@RequestMapping("admin/tixianLog")
public class UsrTixianLogController extends BaseController{

	/**
	 * 我的提现：结构
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="list")
	public ModelAndView list() throws Exception {
		TableHeader tableHeader = new TableHeader();
		tableHeader.setNames(new String[]{"uid", "nick_name","order_id","my_coin:yuan", "coin:yuan","create_time:datetime","acount","status"});
		tableHeader.setTexts(new String[]{"玩家ID","玩家名称", "单号","当前余额","提现金额","申请时间","提现账号","状态"});
		
		Tool tool = new Tool();
		tool.setUrls("admin/tixianLog/toEdit");
		tool.setTexts("修改状态");
		tool.setTypes("edit");
		
		Search search = new Search();
		search.setNames(new String[]{"uid"});
		search.setTexts(new String[]{"玩家ID"});
		search.setTypes(new String[]{"text"});
		
		PageStructure data = PageUtil.createTablePageStructure("admin/tixianLog/listData", "id", tableHeader,tool,search);
		
		Map<String,Object> map = Maps.newHashMap();
		map.put("formStruct", data);
		
		
		return createMV("tableList","提现申请", map);
	}
	
	/**
	 * 我的提现：数据
	 * @param page
	 * @param limit
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="listData")
	public RespVO listData(Integer page,Integer limit,Long uid) throws Exception {
		PageVO users = null;
		if(uid == null) {
			users = DaoUtils.getPageForMap(UsrTixianLog.table(), null,page,limit,Where.eq("method", 3));
		}else {
			users = DaoUtils.getPageForMap(UsrTixianLog.table(), null,page,limit,Where.eq("uid", uid),Where.eq("method", 3));
		}
		
		idToName(users.getItems(), UsrUser.table(), "uid:nick_name");
		idToName(users.getItems(), UsrGameInfo.table(), "uid#uid:coin as my_coin");
		
		return RespVO.createSuccessJsonResonse(users);
	}
	
	/**
	 * 修改状态:界面
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="toEdit")
	public ModelAndView updateStatus(Long id) throws Exception {
		List<FormField> formFieldList = new ArrayList<>();
		formFieldList.add(FormField.builder().name("uid").text("玩家ID").type("span").build());
		formFieldList.add(FormField.builder().name("coin").text("提现金额").type("span").build());
		formFieldList.add(FormField.builder().name("status").text("状态").build());
		UsrTixianLog tixianLog = DaoUtils.getOne(UsrTixianLog.class,Where.eq("id", id));
		Map<String, Object> data = PageUtil.createFormPageStructure("admin/tixianLog/update", formFieldList,tixianLog);
		
		return createCustMV("window/add",data);
	}
	
	/**
	 * 修改状态：执行
	 * @param current_page
	 * @param areas
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="update")
	public RespVO update(UsrTixianLog log) throws Exception {
		DaoUtils.update(log);
		
        return RespVO.createSuccessJsonResonse("修改成功");
	}
	
	
}
